<?php
namespace Boxes\Controller;
use  Common\Controller\BoxesController;
class LoginController extends BoxesController {



    /**
     * 小程序登录
     * @param string
     * @return string
     */
    public function wxlogin(){

        $code =  I('post.code','','htmlspecialchars');
        $appid =  I('post.appid','','htmlspecialchars');
        $secret =  I('post.secret','','htmlspecialchars');

        $nickName = I('post.nickName','','htmlspecialchars');
        $avatarUrl = I('post.avatarUrl','','htmlspecialchars');
        $gender = I('post.gender','','htmlspecialchars');
        $province = I('post.province','','htmlspecialchars');
        $city = I('post.city','','htmlspecialchars');
        $country = I('post.country','','htmlspecialchars');
        $language = I('post.language','','htmlspecialchars');


        if(!$code||!$appid||!$secret){
            $data = array();

            $this->returnData($data,$msg='参数错误',$code=401);

        }

        $url = "https://api.weixin.qq.com/sns/jscode2session?appid=$appid&secret=$secret&js_code=$code&grant_type=authorization_code";

        $res = $this->curlget($url);

        if (!$res['openid']) {
            $this->returnData($res,$msg='openid获取失败',$code=100);
        }


        if ($res['openid']){

            $where= array(
                'openId'=>$res['openid']
            );

            $user = M('boxes_member')->field('boxes_member_id,nickName,gender,city,province,country,avatarUrl,openId')->where($where)->find(); //判断是否有用户信息

            if ($user){
                $user_id = $user['boxes_member_id'];
                $userinfo = array(
                    'nickName'=>$nickName,
                    'gender'=>$gender,
                    'city'=>$city,
                    'province'=>$province,
                    'country'=>$country,
                    'avatarUrl'=>$avatarUrl,
                    'language'=>$language,
                    'uinionid' =>$res['unionId']||'',
                    'logintime' =>time(),
                    'openId' =>$res['openid'],
                    'appid'=>$appid,
                    'session_key'=>$res['session_key'],
                );

                M('boxes_member')->where($where)->save($userinfo);

            }else{

                $user_id= M('boxes_member')->max('boxes_member_id')+1;
                $userinfo = array(
                    'boxes_member_id'=>$user_id,
                    'nickName'=>$nickName,
                    'gender'=>$gender,
                    'city'=>$city,
                    'province'=>$province,
                    'country'=>$country,
                    'avatarUrl'=>$avatarUrl,
                    'language'=>$language,
                    'uinionid' =>$res['unionId']||'',
                    'logintime' =>time(),
                    'openId' =>$res['openid'],
                    'createtime'=>time(),
                    'appid'=>$appid,
                    'session_key'=>$res['session_key'],
                );

                M('boxes_member')->data($userinfo)->add();

            }

        }

        $session_id =   $this->getSessionId();
        //保存数据到session
       // session('userinfo', $userinfo);

        $data = array(
            'openid'=>$res['openid'],
            'session_id' =>$session_id,
            'member_id' =>$user_id
        );

        $this->returnData($data,'成功',200);


    }

    /**
     * 签到
     */
    public function sign()
    {
        $sign_config    =   array('1'=>20,'2'=>30,'3'=>40,'4'=>80,'5'=>100,'6'=>150,'7'=>200);
        $member_id      =   I('post.member_id','','htmlspecialchars');
        $appid          =   I('post.appid','','htmlspecialchars');
        $data           =   array();
        if (! empty($member_id) && ! empty($appid))
        {
            $where                      =   array();
            $where['boxes_member_id']   =   $member_id;
            $where['appid']             =   $appid;
            $field                      =   array('sign_time','sign_num','sign_total','gold','today_gold');
            $member_data                =   M('boxes_member')->where($where)->field($field)->find();
            if (! empty($member_data))
            {
                $now        =   strtotime(date('Y-m-d',time()));
                $yesterday  =   strtotime(date("Y-m-d",strtotime("-1 day")));

                $data       =   array();
                //从来没签到过的
                if (empty($member_data['sign_time']))
                {
                    $update_data                =   array();
                    $update_data['sign_time']   =   $now;
                    $update_data['sign_num']    =   1;
                    $update_data['sign_total']  =   1;
                    $update_data['gold']        =   $sign_config['1'];
                    $update_data['today_gold']  =   $sign_config['1'];
                    $data['gold']               =   $sign_config['1'];
                    $data['total_gold']         =   $sign_config['1'];
                    $result = M('boxes_member')->where($where)->save($update_data);
                }
                else
                {
                    //已经签到过
                    if ($member_data['sign_time'] == $now)
                    {
                        $data['gold']   =   $member_data['today_gold'];
                        $data['total_gold'] =   $member_data['gold'];
                        $data['tip']    =   '今天已经签到过了';
                        $result         =   true;
                    }
                    elseif ($yesterday == $member_data['sign_time'])
                    {

                        $update_data                =   array();
                        $update_data['sign_time']   =   $now;
                        if ($member_data['sign_num'] == 6)
                        {
                            $update_data['sign_num']    =   0;
                            $update_data['gold']        =   $member_data['gold'] + $sign_config[7];
                            $update_data['today_gold']  =   $sign_config[7];
                            $data['gold']               =   $sign_config[7];
                            $data['total_gold']         =   $update_data['gold'];
                        }
                        else
                        {
                            $update_data['sign_num']    =   $member_data['sign_num'] + 1;
                            $update_data['gold']        =   $member_data['gold'] + $sign_config[$update_data['sign_num']];
                            $update_data['today_gold']  =   $sign_config[$update_data['sign_num']];
                            $data['gold']               =   $sign_config[$update_data['sign_num']];
                            $data['total_gold']         =   $update_data['gold'];
                        }
                        $update_data['sign_total']  =   $member_data['sign_total'] + 1;
                        $result =   M('boxes_member')->where($where)->save($update_data);
                    }
                    else
                    {
                        //重新签到
                        $update_data                =   array();
                        $update_data['sign_time']   =   $now;
                        $update_data['sign_num']    =   1;
                        $update_data['sign_total']  =   $member_data['sign_total'] + 1;
                        $update_data['gold']        =   $member_data['gold'] + $sign_config['1'];
                        $update_data['today_gold']  =   $sign_config['1'];
                        $data['gold']               =   $sign_config['1'];
                        $data['total_gold']         =   $update_data['gold'];
                        $result =   M('boxes_member')->where($where)->save($update_data);
                    }
                }

                if (! empty($result))
                {
                    if (empty($data['tip']))
                    {
                        $data['tip']    =   '签到成功';
                    }
                    $this->returnData($data,'签到成功',200);
                }
                else
                {
                    $data           =   array();
                    $data['gold']   =   0;
                    $data['total_gold'] =   $member_data['gold'];
                    $data['tip']    =   '签到失败';
                    $this->returnData($data,'签到失败',400);
                }
            }
            else
            {
                $data           =   array();
                $data['gold']   =   0;
                $data['total_gold'] =   0;
                $data['tip']    =   '用户不存在';
                $this->returnData($data,'签到失败',400);
            }
        }

        $this->returnData($data,'签到失败',400);
    }

    /**
     * 签到状态
     */
    public function sign_status()
    {
        $member_id      =   I('post.member_id','','htmlspecialchars');
        $appid          =   I('post.appid','','htmlspecialchars');

        $data           =   array();
        if (! empty($member_id) && ! empty($appid))
        {
            $where                      =   array();
            $where['boxes_member_id']   =   $member_id;
            $where['appid']             =   $appid;
            $field                      =   array('sign_time','gold','sign_num');
            $member_data                =   M('boxes_member')->where($where)->field($field)->find();
            if (! empty($member_data))
            {
                $now    =   strtotime(date('Y-m-d',time()));
                if (empty($member_data['sign_time']) || $member_data['sign_time'] != $now)
                {
                    $yestoday               =   strtotime("-1 day");
                    if ($yestoday == $member_data['sign_time'])
                    {
                        $data['sign_num']       =   $member_data['sign_num'];
                    }
                    else
                    {
                        $data['sign_num']       =   0;
                    }

                    $data['sign_status']    =   0;
                    $data['gold']           =   $member_data['gold'];
                    $this->returnData($data,'今天还没签到',200);
                }
                elseif ($member_data['sign_time'] == $now)
                {
                    $data['sign_status']    =   1;
                    $data['gold']           =   $member_data['gold'];
                    $data['sign_num']       =   $member_data['sign_num'];
                    $this->returnData($data,'今天已经签到',200);
                }
            }
        }

        $data['sign_status']    =   0;
        $this->returnData($data,'签到失败',400);
    }
}
